﻿using ParboiledSharp.parboiled.dataobjects;

namespace ParboiledSharp.parboiled.rules
{
    class ZeroOrMore : ContainerRule
    {
        private int iterationsCount = 0;

        public ZeroOrMore(object rule)
        {
            SetRule(rule);
        }

        public ZeroOrMore()
        {
        }

        internal override bool Execute(Context context)
        {
            iterationsCount = 0;
            while (true)
            {
                bool result = ExecuteRule(GetRule(), context);
                if (result == false) break;
                iterationsCount++;
            }
          return true;
        }

        public override void EndRule(bool result, Context context) {
            if (result)
            {
                context.SetlastIterationsCount(iterationsCount);
            }
        }
    }
}
